//
//  DHMenuVC.m
//  GoBangGraduationProject
//
//  Created by 刘人华 on 16/5/30.
//  Copyright © 2016年 dahua. All rights reserved.
//

#import "DHMenuVC.h"
#import "LLSwitch.h"
#import "DWBubbleMenuButton.h"

@interface DHMenuVC ()<LLSwitchDelegate>
@property (nonatomic, strong) DHUserModel *user;
@property (nonatomic, strong) DWBubbleMenuButton *downMenuButton;
@property (nonatomic, strong) UIImageView *btnBgmView;
@end

@implementation DHMenuVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupView];
}

- (void)setupView {
    UIImageView *imagView = [[UIImageView alloc] init];
    imagView.image = [UIImage imageNamed:@"bk_gobang_white"];
    [self.view addSubview:imagView];
    [imagView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(imagView.superview).offset(64);
        make.left.right.bottom.equalTo(imagView.superview);
    }];
    
    UILabel *labName = [[UILabel alloc] init];
    labName.textAlignment = NSTextAlignmentCenter;
    labName.textColor = [UIColor whiteColor];
    labName.text = [NSString stringWithFormat:@"  %@ >", self.user.userName];
    labName.backgroundColor = App_Color;
    [self.view addSubview:labName];
    [labName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(labName.superview).offset(150);
        make.left.equalTo(labName.subviews).offset(20);
        make.width.mas_equalTo(SCREEN_W - 100);
        make.height.mas_equalTo(30);
    }];
    
    UILabel *labLevel = [UILabel new];
    labLevel.textAlignment = NSTextAlignmentCenter;
    labLevel.textColor = [UIColor whiteColor];
    labLevel.text = [NSString stringWithFormat:@"  %@ %ld%@", self.user.userLevelDesc, (long)self.user.grade, ATLocalizedString(@"Points", @"分")];
    labLevel.backgroundColor = App_Color;
    [self.view addSubview:labLevel];
    [labLevel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(labName.mas_bottom).offset(20);
        make.left.equalTo(labName.mas_left);
        make.right.equalTo(labName.mas_right);
        make.height.mas_equalTo(30);
    }];
    
    UILabel *labCount = [UILabel new];
    labCount.textAlignment = NSTextAlignmentCenter;
    labCount.textColor = [UIColor whiteColor];
    labCount.text = [NSString stringWithFormat:@"  %@:  %@ %lu  %@ %lu", ATLocalizedString(@"Record", @"战绩"), ATLocalizedString(@"Total", @"总"), (unsigned long)self.user.totalCount, ATLocalizedString(@"Win", @"胜"), (unsigned long)self.user.winCount];
    labCount.backgroundColor = App_Color;
    [self.view addSubview:labCount];
    [labCount mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(labLevel.mas_bottom).offset(20);
        make.left.equalTo(labName.mas_left);
        make.right.equalTo(labName.mas_right);
        make.height.mas_equalTo(30);
    }];
    
    UILabel *labPlayerFirst = [[UILabel alloc] init];
    labPlayerFirst.textAlignment = NSTextAlignmentCenter;
    labPlayerFirst.text = ATLocalizedString(@"Menu_Player_First", @"Player First");
    labPlayerFirst.textColor = [UIColor whiteColor];
    labPlayerFirst.backgroundColor = App_Color;
    [self.view addSubview:labPlayerFirst];
    [labPlayerFirst mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(labCount.mas_bottom).offset(20);
        make.left.equalTo(labPlayerFirst.superview).offset(20);
        make.width.mas_equalTo(120);
        make.height.mas_equalTo(30);
    }];
    
    LLSwitch *switch1 = [[LLSwitch alloc] initWithFrame:CGRectMake(0, 0, 60, 30)];
    switch1.tag = 101;
    switch1.delegate = self;
    switch1.onColor = App_Color;
    switch1.on = [DHNSUserdefault getTheIsPlayerSente];
    [self.view addSubview:switch1];
    [switch1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(60);
        make.height.mas_equalTo(30);
        make.centerY.equalTo(labPlayerFirst);
        make.left.equalTo(labPlayerFirst.mas_right).offset(20);
    }];
    
    _btnBgmView = [[UIImageView alloc] initWithFrame:CGRectMake(0.f, 0.f, 40.f, 40.f)];
    _btnBgmView.backgroundColor = App_Color;
    _btnBgmView.layer.cornerRadius = _btnBgmView.frame.size.width / 2.0f;
    _btnBgmView.clipsToBounds = YES;
    NSInteger index = [DHNSUserdefault getTheUserColor];
    [_btnBgmView setImage:[UIImage imageNamed:ColorNames[index]]];
    self.downMenuButton = [[DWBubbleMenuButton alloc] initWithFrame:CGRectMake(SCREEN_W - 60, 84, 40, 40) expansionDirection:DirectionDown];
    self.downMenuButton.buttonSpacing = SCREEN_H < 570 ? 10 : 20;
    self.downMenuButton.homeButtonView = _btnBgmView;
    [self.downMenuButton addButtons:[self createButtonArray]];
    [self.view addSubview:self.downMenuButton];
    
}

- (void)didTapLLSwitch:(LLSwitch *)llSwitch {
    
}

- (void)animationDidStopForLLSwitch:(LLSwitch *)llSwitch {
    
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    LLSwitch *AISwith = [self.view viewWithTag:101];
    if (llSwitch == AISwith) {
        [defaults setBool:llSwitch.on forKey:IsPlayerSente];
    }
}

#pragma mark - action
- (void)didClickBtn:(UIButton *)sender {
    NSLog(@"Button tapped, imageName: %@", ColorNames[sender.tag]);
    [self.btnBgmView setImage:[UIImage imageNamed:ColorNames[sender.tag]]];
    [DHNSUserdefault setupUserColor:sender.tag];
}

#pragma mark - setter
- (DHUserModel *)user {
    if (!_user) {
        _user = [ATLocal user];
    }
    return _user;
}
- (NSArray *)createButtonArray {
    NSMutableArray *buttonsMutable = [[NSMutableArray alloc] init];
    
    int i = 0;
    for (NSString *imgName in ColorNames) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
        
        [button setBackgroundImage:[UIImage imageNamed:imgName] forState:UIControlStateNormal];
        
        button.frame = CGRectMake(0.f, 0.f, 40.f, 40.f);
        button.layer.cornerRadius = button.frame.size.height / 2.f;
        button.clipsToBounds = YES;
        button.tag = i++;
        
        [button addTarget:self action:@selector(didClickBtn:) forControlEvents:UIControlEventTouchUpInside];
        
        [buttonsMutable addObject:button];
    }
    
    return [buttonsMutable copy];
}

@end
